package com.example.liuke.guangguang.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.liuke.guangguang.browser.KCDefaultBrowser;
import com.example.liuke.guangguang.browser.KCRegistMgr;
import com.kercer.kercore.debug.KCLog;
import com.kercer.kercore.io.KCAssetTool;
import com.kercer.kerkee.bridge.KCJSObject;
import com.kercer.kerkee.bridge.KerkeeMethod;
import com.kercer.kerkee.webview.KCWebPath;

import java.io.File;
import java.io.IOException;

public class KerkeeActivity extends AppCompatActivity {

    public class KCTest extends KCJSObject
    {
        @Override
        public String getJSObjectName()
        {
            return "test";
        }

        @KerkeeMethod
        public String testString()
        {
            return null;
        }

    }


   /*1. 注册js与native对应的类
     2.  实现native部分与js对应的方法
     3. 创建KCWebview进行调用
    */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        setContentView(R.layout.activity_main);

        //copy asset to html dir first
        //if you use zip, you can unzip to html dir
        //复制HTML目录资产第一如果你使用拉链，你可以解压到HTML目录
        if (!isExitAsset())
            copyAssetHtmlDir();


        //create browser that use KCWebview 创建浏览器使用kcwebview
        KCDefaultBrowser browser = new KCDefaultBrowser(this);
        View view = browser.getView();
        setContentView(view);


        //regist classes to JSBridge，the relationship between binding js objects and native classes
        //and you can use default browser'b registJSBridgeClient function
        //注册类jsbridge，结合JS对象和本地类，你可以使用默认的browser'b registjsbridgeclient功能之间的关系
        KCRegistMgr.registClass();
        //you can registObject here;
        //KCJSBridge.registObject(new KCTest());

        browser.loadTestPage();
        //browser.loadUrl("http://www.baidu.com");
    }

    private boolean isExitAsset()
    {
        KCWebPath webPath = new KCWebPath(this);
        String cfgPath = webPath.getCfgPath();
        File file = new File(cfgPath);
        if (file.exists())
            return true;
        return false;
    }

    private void copyAssetHtmlDir()
    {
        KCAssetTool assetTool = new KCAssetTool(this);
        try
        {
            assetTool.copyDir("html", new KCWebPath(this).getResRootPath());
        }
        catch (IOException e)
        {
            KCLog.e(e);
        }
    }
}
